var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/functionalComponent/table/child/filter.tsx
var filter_exports = {};
__export(filter_exports, {
  FilterIcon: () => FilterIcon
});
module.exports = __toCommonJS(filter_exports);
var import_icons = require("@ant-design/icons");
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_baseComponent = require("../../../baseComponent");
var import_Button = require("../../antd/Button");
var import_util = require("../util");
function isEmpty(keyword) {
  return import_util.util.isNullOrEmpty(keyword) || import_util.util.isArray(keyword) && keyword.length === 0;
}
var FilterIcon = import_react.default.memo(({ table, column }) => {
  const {
    state: {
      params: { pageIndex, pageSize }
    }
  } = table;
  const dataIndexMap = table.getDataIndexMap();
  const { dataIndex, dataIndexField, filter, title, render } = column;
  const [visible, setVisible] = (0, import_react.useState)(false);
  const [filterValue, setFilterValue] = (0, import_react.useState)("");
  const [filtered, setFiltered] = (0, import_react.useState)(false);
  const [{ index: findIndex }, setFind] = (0, import_react.useState)({ index: -1 });
  const innerRef = (0, import_react.useRef)({ lastValue: "", oType: "" });
  const searchInput = (0, import_util.useApi)();
  const selectOptions = (0, import_react.useRef)();
  const di = dataIndexField || dataIndex;
  const filterType = filter.type ? import_util.util.isArray(filter.type) ? filter.type : [filter.type] : ["filter", "find"];
  (0, import_util.useUpdateEffect)(() => {
    innerRef.current.oType = "";
    setFind((prev) => prev.index !== -1 ? { index: -1 } : prev);
  }, [filterValue]);
  (0, import_react.useEffect)(() => {
    if (!filtered && selectOptions.current) {
      selectOptions.current = null;
    }
  }, [filtered]);
  (0, import_react.useEffect)(() => {
    return table.subscribe(() => {
      setFiltered(false);
    }, "onDataLoad");
  }, []);
  const handleVisibleChange = (flag) => {
    setVisible(flag);
    if (flag) {
      setFilterValue(innerRef.current.lastValue);
      setTimeout(() => {
        var _a, _b, _c;
        return (_c = (_a = searchInput.current) == null ? void 0 : (_b = _a.getApi()).select) == null ? void 0 : _c.call(_b);
      }, 100);
    }
  };
  const closeDropdown = (0, import_util.useRefCallback)(() => {
    setVisible(false);
  });
  const generateParams = ({ columnFilters = {}, ...others }, keyword) => {
    const fieldName = filter.name || dataIndex;
    if (isEmpty(keyword)) {
      delete columnFilters[fieldName];
    } else {
      columnFilters[fieldName] = keyword;
    }
    return {
      ...others,
      columnFilters: { ...columnFilters }
    };
  };
  const asyncFilter = (keyword) => {
    return new Promise((resolve) => {
      table.query(
        (params) => generateParams(params, keyword),
        (r) => {
          resolve(r);
        }
      );
    });
  };
  const onFilter = (0, import_util.useRefCallback)(async ({ type = "filter" }) => {
    if (!filterType.includes(type)) {
      return;
    }
    innerRef.current.lastValue = filterValue;
    innerRef.current.oType = type;
    if (type === "find") {
      if (!import_util.util.isNullOrEmpty(filterValue)) {
        const [find] = await table.findKeyword({
          keyword: filterValue,
          dataIndex: di,
          currentIndex: findIndex
        });
        setFind({ index: find });
      }
    } else {
      if (filter.remoteFilter && table.props.request) {
        await asyncFilter(filterValue);
      } else {
        const { columnFilters } = generateParams(table.state.params, filterValue);
        table.filter((0, import_util.clientColumnFilter)(columnFilters, dataIndexMap));
      }
      setFiltered(!isEmpty(filterValue));
    }
  });
  const onReset = (0, import_util.useRefCallback)(() => {
    setFilterValue("");
  });
  const getDataOptions = (0, import_util.useRefCallback)(() => {
    if (!selectOptions.current) {
      const ws = /* @__PURE__ */ new Set();
      const result = [];
      const loop = (rows) => {
        rows.forEach(({ children, ...row }) => {
          const value = import_util.util.getObjValue(row, di);
          if (!ws.has(value)) {
            ws.add(value);
            result.push({
              value,
              label: render ? render({
                table,
                row,
                value,
                dataIndex,
                rowIndex: table.getRowIndex(row),
                pageIndex,
                pageSize
              }) : value
            });
          }
          (children == null ? void 0 : children.length) && loop(children);
        });
      };
      loop(table.getStore().data);
      selectOptions.current = result;
    }
    return selectOptions.current;
  });
  const getDefaultInput = (0, import_util.useRefCallback)(() => {
    const { type = "text", options } = filter.inputProps || {};
    if (type === "select") {
      const selectProps = {
        value: filterValue,
        style: { width: "100%" },
        placeholder: `请选择${title || dataIndex}`,
        onChange: (value) => {
          setFilterValue(value == null ? void 0 : value.value);
        }
      };
      if (options) {
        if (import_util.util.isFunction(options)) {
          selectProps.request = () => options({ dataIndex });
        } else {
          selectProps.data = options;
        }
      } else {
        selectProps.request = getDataOptions;
      }
      return /* @__PURE__ */ import_react.default.createElement(import_baseComponent.Select, { ref: searchInput, ...selectProps, getPopupContainer: (c) => c });
    }
    if (type === "checkbox") {
      const checkboxProps = {
        value: filterValue,
        onChange: (value) => {
          setFilterValue(value);
        }
      };
      if (options) {
        checkboxProps.data = import_util.util.isFunction(options) ? options({ dataIndex }) : options;
      } else {
        checkboxProps.data = getDataOptions();
      }
      return /* @__PURE__ */ import_react.default.createElement(import_baseComponent.CheckboxGroup, { ref: searchInput, ...checkboxProps });
    }
    return /* @__PURE__ */ import_react.default.createElement(
      import_baseComponent.Input,
      {
        ref: searchInput,
        value: filterValue,
        type,
        onChange: (v) => {
          setFilterValue(v);
        },
        onPressEnter: () => onFilter({ type: filterType.includes("find") ? "find" : "filter" }),
        placeholder: `请输入${title || dataIndex}`
      }
    );
  });
  const dropdownRender = (0, import_util.useRefCallback)(() => /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      style: {
        backgroundColor: "var(--component-background)",
        boxShadow: "0 3px 6px -4px rgba(0,0,0,.12), 0 6px 16px 0 rgba(0,0,0,.08), 0 9px 28px 8px rgba(0,0,0,.05)",
        borderRadius: "var(--border-radius)"
      }
    },
    /* @__PURE__ */ import_react.default.createElement("div", { style: { padding: 8 } }, filter.dropdown ? filter.dropdown({
      closeDropdown,
      table,
      column,
      onFilter,
      onReset,
      filterValue,
      setFilterValue,
      filtered
    }) : /* @__PURE__ */ import_react.default.createElement("div", { style: { minWidth: 200 } }, getDefaultInput(), /* @__PURE__ */ import_react.default.createElement("div", { style: { lineHeight: "18px", fontSize: 12, height: `${filterType.includes("find") ? 18 : 0}px` } }, filterType.includes("find") && !import_util.util.isNullOrEmpty(filterValue) && innerRef.current.oType === "find" ? findIndex === -1 ? `未找到${filterValue}` : `第${findIndex + 1}行` : ""), /* @__PURE__ */ import_react.default.createElement("div", { style: { display: "flex", alignItems: "center" } }, /* @__PURE__ */ import_react.default.createElement("a", { onClick: onReset, style: { flex: 1 } }, "重置"), filterType.includes("find") && /* @__PURE__ */ import_react.default.createElement(
      import_Button.Button,
      {
        className: "btn-compact",
        style: { marginLeft: 8 },
        size: "small",
        type: "primary",
        icon: /* @__PURE__ */ import_react.default.createElement(import_icons.EnvironmentOutlined, null),
        onClick: () => onFilter({ type: "find" })
      },
      "定位"
    ), filterType.includes("filter") && /* @__PURE__ */ import_react.default.createElement(
      import_Button.Button,
      {
        className: "btn-compact",
        style: { marginLeft: 8 },
        size: "small",
        type: "primary",
        icon: /* @__PURE__ */ import_react.default.createElement(import_icons.SearchOutlined, null),
        onClick: () => onFilter({ type: "filter" })
      },
      "筛选"
    ))))
  ));
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: "header-icon",
      style: { fontSize: 12, fontWeight: 500, padding: 0, margin: "0 0 0 2px" },
      onClick: import_util.stopPropagation
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_antd.Popover,
      {
        content: dropdownRender,
        placement: "bottomRight",
        trigger: ["click"],
        overlayStyle: { padding: 0 },
        overlayInnerStyle: { padding: 0 },
        open: visible,
        onOpenChange: handleVisibleChange
      },
      /* @__PURE__ */ import_react.default.createElement("span", { style: { lineHeight: "20px", padding: "0 4px", color: filtered ? import_util.cssVar.primaryColor : void 0 } }, filter.icon ? filter.icon({ filtered, color: filtered ? import_util.cssVar.primaryColor : void 0 }) : /* @__PURE__ */ import_react.default.createElement(import_icons.FilterFilled, null))
    )
  );
});
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  FilterIcon
});
